package com.example.win.android_banduyujia.peopleactivity.ui.image;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.LinearInterpolator;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.win.android_banduyujia.R;
import com.example.win.android_banduyujia.peopleactivity.ui.model.PhotoModel;
import com.example.win.android_banduyujia.peopleactivity.ui.util.AnimationUtil;
import com.example.win.android_banduyujia.peopleactivity.ui.view.PhotoPreview;

import java.util.List;

/**
 * Created by Win on 2015/9/17.
 */
public class BasePhotoPreviewActivity extends Activity implements ViewPager.OnPageChangeListener, View.OnClickListener {

    private ViewPager mViewPager;
    private RelativeLayout  layoutTop;
    private ImageButton     btnBack;
    private TextView        tvPercent;
    protected List<PhotoModel> photos;
    protected int current;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
        setContentView(R.layout.layout_photopreview);
        layoutTop = (RelativeLayout)findViewById(R.id.layout_top_app);
        btnBack = (ImageButton)findViewById(R.id.btn_back_app);
        tvPercent=(TextView)findViewById(R.id.tv_percent_app);
        mViewPager=(ViewPager)findViewById(R.id.vp_base_app);

        btnBack.setOnClickListener(this);
        mViewPager.setOnPageChangeListener(this);

        overridePendingTransition(R.anim.activity_alpha_action_in, 0); // 渐入效果
    }

    /** 绑定数据，更新界面 */
    public void bindData() {
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.setCurrentItem(current);
    }

    private PagerAdapter mPagerAdapter = new PagerAdapter() {
        @Override
        public int getCount() {
            if (photos == null) {
                return 0;
            } else {
                return photos.size();
            }
        }

        @Override
        public boolean isViewFromObject(View view, Object o) {
            return view == o;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            PhotoPreview photoPreview = new PhotoPreview(getApplicationContext());
            ((ViewPager)container).addView(photoPreview);
            photoPreview.loadImage(photos.get(position));
            photoPreview.setOnClickListener(photoItemClickListener);
            return photoPreview;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    };

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

    @Override
    public void onPageSelected(int i) {
        current = i;
        updatePercent();
    }

    protected void updatePercent() {
        tvPercent.setText((current + 1) + "/" + photos.size());
    }

    protected boolean isUp;
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_back_app)
            finish();
    }

    /** 图片点击事件回调 */
	private View.OnClickListener photoItemClickListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			if (!isUp) {
				new AnimationUtil(getApplicationContext(), R.anim.translate_up)
						.setInterpolator(new LinearInterpolator()).setFillAfter(true).startAnimation(layoutTop);
				isUp = true;
			} else {
				new AnimationUtil(getApplicationContext(), R.anim.translate_down_current)
						.setInterpolator(new LinearInterpolator()).setFillAfter(true).startAnimation(layoutTop);
				isUp = false;
			}
		}
	};
}
